package com.af.community.smart.business

import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import com.af.community.smart.base.*
import com.af.community.smart.model.MainData
import com.af.community.smart.utils.MultiGapDecoration
import com.af.community.smart.utils.dp2px
import com.af.community.smart.utils.scaleImage
import com.af.community.smart.viewmodel.UpdateViewModel
import com.sunfusheng.GlideImageView
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import net.idik.lib.slimadapter.SlimAdapterEx
import org.jetbrains.anko.find
import org.jetbrains.anko.include
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import org.koin.androidx.viewmodel.ext.android.getViewModel

class MainActivity : BaseActivity() {

    private var overallXScroll = 0
    private lateinit var mainImg: GlideImageView
    private lateinit var mainName: TextView
    private lateinit var mainArea: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        include<View>(R.layout.layout_list)
        setTransparentAndToolbar(isDarkFont = false)
        initTitle()

        mAdapterEx.updateData(mList.apply {
            add("人户采集")
            add(MainData("人员信息", R.mipmap.icon_main01, "people"))
            add(MainData("房屋信息", R.mipmap.icon_main02, "house"))
            add("网格事件管理")
            add(MainData("网格事件上报", R.mipmap.icon_main03, "report"))
            add(MainData("事件处理查询", R.mipmap.icon_main04, "reportList"))
            add("社区业务审核")
            add(MainData("办事指南查询", R.mipmap.icon_main05, "handle"))
            add(MainData("业务事项初审", R.mipmap.icon_main06, "initial"))
            add(MainData("办件进展查询", R.mipmap.icon_main07, "speed"))
            add(MainData("自由申报审核", R.mipmap.icon_main08, "verify"))
            add("日常办公")
            add(MainData("待办任务", R.mipmap.icon_main09, "task"))
            add(MainData("通知公告", R.mipmap.icon_main10, "notice"))
            add(MainData("日常巡检", R.mipmap.icon_main11, "patrol"))
            add(MainData("考勤签到", R.mipmap.icon_main12, "sign"))
            add(MainData("通讯录", R.mipmap.icon_main13, "contact"))
            add(MainData("民情日志", R.mipmap.icon_main14, "daily"))
        })

        getViewModel().checkUpdate(this, slience = true)
    }

    override fun getViewModel() = getViewModel<UpdateViewModel>()

    override fun initTitle(title: String, name: String) {
        swipe_refresh.isEnabled = false
        recycle_list.loadGrid {
            addItemDecoration(MultiGapDecoration(16))
            layoutManager = GridLayoutManager(baseContext, 2).apply {
                spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
                    override fun getSpanSize(position: Int) = when {
                        mAdapterEx.getItem(position) is MainData -> 1
                        else -> 2
                    }
                }
            }

            addScrollListener {
                onScrolled { _, _, dy ->
                    overallXScroll += dy // dy:每一次竖直滑动增量 向下为正 向上为负
                    when {
                        overallXScroll < dp2px(180f) -> {
                            transparentStatusBar(isDarkFont = false)
                        }
                        else -> transparentStatusBar(isDarkFont = true)
                    }
                }
            }
        }

        val mHeader = inflate<View>(R.layout.header_main)
        mainImg = mHeader.find(R.id.main_img)
        mainName = mHeader.find(R.id.main_name)
        mainArea = mHeader.find(R.id.main_area)

        mAdapterEx = SlimAdapter.create(SlimAdapterEx::class.java)
            .addHeader(mHeader)
            .register<String>(R.layout.item_main_divider) { data, injector ->
                injector.text(R.id.item_main_title, data)
            }
            .register<MainData>(R.layout.item_main_list) { data, injector ->

                //解构声明
                val (innerTitle, image, flag) = data

                injector.image(R.id.item_main_img, image)
                    .text(R.id.item_main_name, innerTitle)
                    .clicked(R.id.item_main) {
                        when (flag) {
                            "people" -> startActivity<PeopleActivity>()
                            "house" -> startActivity<HouseActivity>()
                            "report" -> startActivity<ReportActivity>()
                            "reportList" -> startActivity<ReportListActivity>()
                            "handle" -> startActivity<HandleActivity>()
                            "initial" -> {
                                toast("正在开发升级中！")
                                // startActivity<InitialActivity>()
                            }
                            "speed" -> {
                                toast("正在开发升级中！")
                                // startActivity<SpeedActivity>()
                            }
                            "verify" -> startActivity<VerifyActivity>()
                            "task" -> startActivity<TaskActivity>()
                            "notice" -> startActivity<NoticeActivity>()
                            "patrol" -> startActivity<PatrolActivity>()
                            "sign" -> startActivity<SignActivity>()
                            "contact" -> startActivity<ContactActivity>()
                            "daily" -> startActivity<DailyActivity>()
                        }
                    }
            }
            .attachTo(recycle_list)
    }

    override fun onStart() {
        super.onStart()
        mainImg.loadCircleImage(getString("avatar").scaleImage())
        mainName.text = getString("userName")
        mainArea.text = getString("gridName")
    }

    override fun doClick(v: View) {
        super.doClick(v)
        when (v.id) {
            R.id.main_setting -> startActivity<SettingActivity>()
            R.id.main_head -> startActivity<InfoActivity>()
        }
    }

    private var exitTime: Long = 0
    override fun onBackPressed() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            toast("再按一次退出程序")
            exitTime = System.currentTimeMillis()
        } else super.onBackPressed()
    }

}
